// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    url: '',
    openId: '',
    id: '',
    uuid: '',
    phoneStatus: '0'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    wx.setNavigationBarTitle({
      title: '泛微产品体验',
    })
    let id = this.getQueryVariable(decodeURIComponent(options.scene), 'id')
    let uuid = this.getQueryVariable(decodeURIComponent(options.scene), 'key')
    this.setData({
      id,
      uuid
    })
    var that = this
    wx.login({
      success (res) {
        if (res.code) {
          wx.request({
            url: 'https://achievement.e-cology.com.cn/democenter/authorize.jsp',
            method: 'GET',
            data: {
              id:id,
              key: uuid,
              status: '0',
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              if (res.data && res.data.data) {
                that.setData({openId: res.data.data, phoneStatus:res.data.phoneStatus })
              }
            }
          })

        } else {
          
        }
      }
    })
  },

  // 获取url参数 
  getQueryVariable(url, variable) {
    if (!url) return ''
    var query = url;
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable) return pair[1]; // 没有花括号默认执行第一个js语句
    }
    return(false);
  },

  cancel () {
    wx.reLaunch({
      url: `/pages/privilegeGrantFailed/index?id=${this.data.id}&key=${this.data.uuid}`
    })
  },
  confirm () {
    var _this = this
    wx.login({
      success (res) {
        if (res.code) {
          wx.request({
            url: 'https://achievement.e-cology.com.cn/democenter/authorize.jsp',
            method: 'GET',
            data: {
              id:_this.data.id,
              key: _this.data.uuid,
              status: '1',
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              if (res.data && res.data.status == '1') {
                wx.reLaunch({
                  url: '/pages/leadpath/index'
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '登录失败，请联系泛微销售协助体验Demo',
                  success (res) {
                    if (res.confirm) {
                    } else if (res.cancel) {
                    }
                  }
                })
              }
            }
          })
        } else {
        }
      }
    })
  },
  getPhoneNumber (e) {
    if (e.detail.errMsg != "getPhoneNumber:ok") return
    var _this = this
    wx.login({
      success (res) {
        if (res.code) {
          wx.request({
            url: 'https://achievement.e-cology.com.cn/democenter/authorize.jsp',
            method: 'GET',
            data: {
              id:_this.data.id,
              key: _this.data.uuid,
              status: '1',
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
            }
          })
        } else {
        }
      }
    })
    wx.request({
      url: 'https://achievement.e-cology.com.cn/democenter/authorizeInfo.jsp',
      method: 'GET',
      data: {
        openId: _this.data.openId,
        code: e.detail.code
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if (res.data && res.data.status == '1') {
          wx.reLaunch({
            url: '/pages/leadpath/index'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '登录失败，请联系泛微销售协助体验Demo',
            success (res) {
              if (res.confirm) {
              } else if (res.cancel) {
              }
            }
          })
        }
      }
    })
  },

  // 生成uuid
  guid() {
    return 'xxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
  }
})